package iris_controller

import (
	"github.com/kataras/iris/v12"
	"gluenet/modules/manager/server/domain"
	"gluenet/modules/manager/server/infra"
	"gluenet/modules/manager/server/infra/prepare"
)

func init() {
	prepare.RegisterAuthController(func() {
		controller := NewOrganizeController()
		prepare.InjectAuthController(func() *Organize {
			return controller
		})
	})
}

type Organize struct {
	Organize *domain.Organize
}

func NewOrganizeController() *Organize {
	r := &Organize{}
	prepare.FetchDependency(&r.Organize)
	return r
}

// Get serves
// Method:   GET
// Resource: http://localhost:8080/apis/organize/tree
func (c *Organize) GetTree(ctx iris.Context) *infra.Response {
	objs, err := c.Organize.GetTree()
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(objs)
}

// Get serves
// Method:   GET
// Resource: http://localhost:8080/apis/organize/tree/build
func (c *Organize) GetTreeBuild(ctx iris.Context) *infra.Response {
	err := c.Organize.BuildTree()
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(nil)
}

// Get serves
// Method:   GET
// Resource: http://localhost:8080/apis/organize/tree/clean
func (c *Organize) GetTreeClean(ctx iris.Context) *infra.Response {
	err := c.Organize.CleanTree()
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(nil)
}
